/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 1/17/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define DMXeDao class. This class inherit from  DMXeDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class DMXeDao : DMXeDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public DMXeDao() { }
        static DMXeDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(DMXeDao).FullName);
        }
        /// <summary>
        /// Define function Update for DMXe Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Update(DMXe obj)
        {
            bool result = false;
            String methodName = "UpdateDMXe";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdateDMXe");


            //Add value "MaXe"  paramater.
            ManagerDao.AddInParameter(cmd, "MaXe", DbType.Int32, obj.MaXe);
            //Add value "TenXe"  paramater.
            ManagerDao.AddInParameter(cmd, "TenXe", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.TenXe));
            //Add value "ChungXe"  paramater.
            ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.ChungXe));
            //Add value "IsPublished"  paramater.
            ManagerDao.AddInParameter(cmd, "IsPublished", DbType.Boolean, ConvertValueObject.CastObjValueToInsertDB(obj.IsPublished));
            //Add value "Order"  paramater.
          
            //Add value "LastUpdatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
            //Add value "LastUpdatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        public static bool Insert(DMXe obj)
        {
            bool result = false;
            String methodName = "InsertDMXe";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertDMXe");

            //Add value "TenXe"  paramater.
            ManagerDao.AddInParameter(cmd, "TenXe", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.TenXe));
            //Add value "ChungXe"  paramater.
            ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.ChungXe));
            //Add value "IsPublished"  paramater.
            ManagerDao.AddInParameter(cmd, "IsPublished", DbType.Boolean,
                ConvertValueObject.CastObjValueToInsertDB(obj.IsPublished));
           
            //Add value "CreatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
            //Add value "CreatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime,
                ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
            //Add value "LastUpdatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
            //Add value "LastUpdatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime,
                ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        public static bool Publish(DMXe obj)
        {
            bool result = false;
            String methodName = "Publish";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spPublishDMXe");

            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "MaXe", DbType.Int32, obj.MaXe);

            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        public static bool Order(DMXe obj, int type)
        {
            bool result = false;
            String methodName = "Order";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spOrderDMXe");

            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "MaXe", DbType.Int32, obj.MaXe);
            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "Type", DbType.Int32, type);


            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
    }
}
        #endregion